/**
 * Created by zhulizhe on 2017/8/18.
 */
import {observable, action, computed} from 'mobx'
import RequestUtil from "../util/NetUtil"


// 普通商品模型
class ProductItemModel {
    constructor(item) {
        this.productItemId = item.productItemId;
        this.originalPrice = item.originalPrice;
        this.currentPrice = item.currentPrice;
        this._selectCount = item.selectCount;
        this.saleMount = item.saleMount;
        this.name = item.name;
    }

    @observable _selectCount = 0;

    @computed
    get selectCount() {
        return this._selectCount;
    }

    @action
    updateCount(count) {
        this._selectCount = count;
    }
}

// 桶装水模型
class ProductItem {

    constructor(name, imageUrl, type, pModels = []) {
        this.name = name;
        this.imageUrl = imageUrl;
        this.type = type;
        this._pItemModels = pModels.map(pM => {
            return new ProductItemModel(pM);
        })
    }

    @observable _pItemModels = [] ;

    @computed get pItemModels(){
        return this._pItemModels ;
    }

    @observable _activePolicyIndex=0;//水票套餐选中

    @computed get activePolicyIndex(){
        return this._activePolicyIndex;
    }

    @computed get activeProductItem(){
        return this._pItemModels[this.activePolicyIndex]
    }

    @action setActivePolicyIndex(i){
        this._activePolicyIndex=i;
    }

    resetProduct(){
        this._pItemModels.forEach(pM=>{
            pM.updateCount(0);
        })
    }
}


// 商品分类模型
class ProductCategoryItem {

    @observable tagSelected = false;

    constructor(id, name, products, _selectCount = 0) {
        this.name = name;
        this._productList = products;
        this._selectCount = _selectCount;
        this.id = id;
    }

    @observable _selectCount = 0;

    // 总数量
    @computed
    get selectCount() {
        return this._selectCount;
    }

    // 商品列表
    @observable _productList = [];

    @computed
    get productList() {
        return this._productList;
    }

    @computed
    get selected() {
        return this._selectCount > 0;
    }

    // 更新当前tag总数量
    @action
    updateSelectCount(count) {
        this._selectCount = count;
    }

    //重置商品数量

    @action
    resetProduct(){
        this._productList.forEach(p=>{
            p.resetProduct();
        })
        this.updateSelectCount(0);
    }
}


// 水站店铺Store
class WaterShopStore {

    @observable productListStore;
    @observable _slectedItem=new ProductItem('', '', '', [{currentPrice:0}]);

    constructor() {
        // 所有的商品列表
        this.products = [];
    }

    // 店铺商品列表
    @observable _tagItems = [];

    // 获取购物车所有商品
    @computed
    get tagItems() {
        return this._tagItems;
    }

    // 选中的购物车商品
    @observable _selectedItemList = [];

    @computed
    get selectedItemList() {
        return this._selectedItemList;
    }

    // 商品总数量
    @observable _totalCount = 0;

    // 获取购物车商品总数量
    @computed
    get totalCount() {
        return this._totalCount;
    }

    // 商品总价格
    @observable _totalPrice = 0;

    // 获取购物车商品总价格
    @computed
    get totalPrice() {
        return this._totalPrice;
    }

    @computed get getSelectedItem(){
        return this._slectedItem;
    }

    @action
    initStoreWithShopId(shopId) {
        // 重置商品列表
        this.products = [];
        // 店铺商品列表
        this.requestShopProductList(shopId);
        // 选中的商品列表
        this.requestSelctedProductList(shopId);
    }

    // 店铺商品列表
    requestShopProductList(shopId) {
        RequestUtil.requestData('shop/shoppingcart/productList', {
            shopId: shopId
        }).then((productListM) => {
            if (productListM != undefined) {
                this.createProductItems(productListM);
            }
        });
    }

    // 选中的商品列表
    requestSelctedProductList(shopId) {
        LOG('products----------' + JSON.stringify(this.products));
        RequestUtil.requestData('shop/shoppingcart/selectedProductList', {
            shopId: shopId
        }).then((selectedProductList) => {
            if (selectedProductList != undefined) {
                let {selectCount, totalPrice, productItemModels} = selectedProductList;
                let Items = productItemModels.map(m => {
                    let shoppingCartItem = this.findProduct(m);
                    // 纯粹的商品
                    return shoppingCartItem;
                })
                this._selectedItemList = Items;
                LOG('购物车--------'+JSON.stringify(this._selectedItemList));
                this.update(selectCount, totalPrice);
            }
        })
    }

    // 初始化当前店铺
    createProductItems(productListM) {
        this.products=[];
        let {totalCount, totalPrice, tagModelList} = productListM;
        this._totalCount = totalCount;
        this._totalPrice = totalPrice;

        // 获取商品ID
        const getProductItemId = (pItem => {
            let pItemModel = pItem.activeProductItem;
            return pItemModel.productItemId;
        })

        // 查询是否存在该商品
        const existProduct = (pItem) => {
            for (let i in this.products) {
                let p = this.products[i];
                if (getProductItemId(p) == getProductItemId(pItem)) {
                    return p;
                    break;
                }
            }
            return null;
        }

        let tagProducts = [];
        tagModelList.map(tagM => {
            let productList = [];
            tagM.productList.map((product) => {
                let pItem = new ProductItem(product.name, product.imageUrl, product.type, product.productItemModels);
                LOG('处理后pItem------------'+JSON.stringify(pItem));
                let existItem = existProduct(pItem);
                if (existItem != null) {
                    productList.push(existItem);
                }
                else {
                    productList.push(pItem);
                }
                // 放入商品列表
                this.products.push(pItem);
            })
            let tagItem = new ProductCategoryItem(tagM.id, tagM.name, productList, tagM.selectCount);
            tagProducts.push(tagItem);
        })
        this._tagItems = tagProducts;
    }

    // 生成订单后重置购物车
    @action resetShoppingCart(){
        // 重置总价总数量
        this.update(0,0) ;
        // 重置商品列表
        this._tagItems.forEach((tag=>{
            tag.resetProduct();
        }))
        // 重置购物车数据
        this._selectedItemList=[];
    }
    @action setSelectedItem(item){
        this._slectedItem=item;
    }

    @action
    update(selectCount, totalPrice) {
        this._totalCount = selectCount; //更新购物车商品总数量
        this._totalPrice = totalPrice;//更新购物车商品总价格
    }

    @action
    updateProduct(item, resultMount) {
        // 更新商品
        let p = item.activeProductItem;
        p.updateCount(resultMount);
    }

    @action
    updateTagList(totalPrice, totalCount, tagItemModelList) {
        this._totalCount = totalCount; //更新购物车商品总数量
        this._totalPrice = totalPrice;//更新购物车商品总价格
        tagItemModelList.map(tagM => {
            LOG('##tagM', JSON.stringify(tagM));
            this.tagItems.map(tagItem => {
                LOG('##tagItem', JSON.stringify(tagItem));
                if (tagItem.id == tagM.tagId) {
                    tagItem.updateSelectCount(tagM.tagMount);
                }
            })
        });
    }

    // 店铺商品列表查找商品
    findProduct(m) {
        for(let index in this.products){
            let productM = this.products[index];
            for (var i=0;i<productM.pItemModels.length;i++){
                if(productM.pItemModels[i].productItemId == m.productItemId){
                    return productM ;
                }
            }
        }
    }

    // 清空购物车
    clearShoppingCart(shopId){
        RequestUtil.requestData('shop/shoppingcart/clear', {
            shopId: shopId
        }).then((result)=>{
            this.resetShoppingCart();
        })
    }
}

const waterShopStore = new WaterShopStore();
module.exports = waterShopStore;